package com.xiaotu.spider.qq.pageprocessor;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.HotTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 QQHotPageProcessor
 * @日期 2017年2月13日
 * @作者 高海军
 * @功能 腾讯视频网站热剧爬取
 */
@Component("QQHotPageProcessor")
@Scope("prototype")
public class QQHotPageProcessor extends AUrlPageProcessor
{
    @Override
    protected String[] getStartRequestUrls()
    {
        return new String[]{
                "http://v.qq.com/x/list/tv?iarea=814&offset=0&sort=18", // 内地最近热播
                "http://v.qq.com/x/list/tv?iarea=814&offset=0&sort=19",// 内地最新上架
                "http://v.qq.com/x/list/tv?iarea=817&sort=18&offset=0",//台湾最近热播
                "http://v.qq.com/x/list/tv?sort=19&offset=0&iarea=817"//台湾最新上架
        };
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.QQ_HOT;
    }

    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.SEARCH_LIST;
    }

    @Override
    public void process(Page page)
    {
        List<String> divList = page.getHtml()
                .xpath("//div[@class='mod_figures_wrapper']/div[@class='mod_bd']//ul[@class" +
                        "='figures_list']/li[@class='list_item']")
                .all();
        if (divList == null || divList.isEmpty())
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);

        List<HotTVModel> tvList = new ArrayList<HotTVModel>();
        for (String divStr : divList)
        {
            Html divObj = new Html(divStr);
            tvList.add(new HotTVModel(Constants.SiteName.QQTV,
                    divObj.xpath(
                            "//div[@class='figure_title_score']/strong/a/text()")
                            .get(),
                    divObj.xpath("//div[@class='figure_desc']/a/@title").all(),
                    divObj.xpath(
                            "//div[@class='figure_title_score']/strong/a/@href")
                            .get()));
        }
        page.putField(HotTVModel.class.getName(), tvList);

        String current = page.getHtml()
                .xpath("//div[@class='mod_pages']/span/a[@class='page_num current']/text()")
                .get();
        if (StringUtils.isNotEmpty(current))
        {
            current = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, current);
            if (StringUtils.isNotEmpty(current))
            {
                if (PropertiesUtil.getPropertyByInt(
                        Constants.SpiderConfigKey.HOTTV_PAGE_SIZE) > Integer
                        .parseInt(current))
                    PageUtils.addTargetRequest(page,
                            page.getHtml()
                                    .xpath("//div[@class='mod_pages']/a[@class='page_next']/@href")
                                    .get(),
                            page.getRequest().getExtras());
            }
        }
    }
}
